package com.wisdytech.ccdserver.common.utils;

import org.springframework.mock.web.MockMultipartFile;
import org.springframework.web.multipart.MultipartFile;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;

/**
 * 文件帮助类
 *
 * @author svolt
 */
public class FileUtils {

    public static MultipartFile writeContentToFile(String content,String fileName) throws Exception {

        MultipartFile multipartFile = null;
        ByteArrayOutputStream outputStream = new ByteArrayOutputStream();;
        ByteArrayInputStream inputStream = null;
        try{
            // 将字符串转换为字节数组，并写入到字节数组输出流中
            outputStream.write(content.getBytes("UTF-8"));
            byte[] bytes = outputStream.toByteArray();
            inputStream = new ByteArrayInputStream(bytes);
            multipartFile = new MockMultipartFile(fileName, fileName + ".txt", "text/plain", inputStream);
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            outputStream.close();
        }
        return multipartFile;
    }
}
